package com.farm.kaiping.controller;


import com.farm.common.security.AnonymousAccess;
import com.farm.kaiping.entity.SysFile;
import com.farm.kaiping.po.SysFilePO;
import com.farm.kaiping.service.SysFileService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件管理表(SysFile)表控制层
 *
 * @author zhuhui
 * @since 2024-11-26 22:58:17
 */
@RestController
@RequestMapping("/sysFile")
@AllArgsConstructor
@Api(tags = "文件管理表控制器")
public class SysFileController {

	private final SysFileService sysFileService;

	@ApiOperation(value = "分页查询文件管理表", notes = "权限：view_sysFile_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysFile_page')")
	@PostMapping("/page")
	public R<Page<SysFile>> getSysFilePage(@RequestBody SysFilePO sysFilePO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(sysFilePO);
		return R.ok(sysFileService.getSysFilePage(page, sysFilePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询文件管理表列表", notes = "权限：view_sysFile_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysFile_list')")
	@PostMapping("/list")
	public R<List<SysFile>> getSysFileList(@RequestBody SysFilePO sysFilePO) {
		try{
			return R.ok(sysFileService.getSysFileList(sysFilePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询文件管理表", notes = "权限 view_sysFile_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysFile_view')" )
	public R getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(sysFileService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条文件管理表", notes = "权限：view_sysFile_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysFile_one')")
	@PostMapping("/one")
	public R<SysFile> getSysFile(@RequestBody SysFilePO sysFilePO) {
		try{
			return R.ok(sysFileService.getSysFile(sysFilePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增文件管理表", notes = "权限：view_sysFile_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysFile_add')")
	@PostMapping("/add")
	public R saveSysFile(@RequestBody SysFilePO sysFilePO) {
		try{
		    return R.ok(sysFileService.saveSysFile(sysFilePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id删除文件管理表", notes = "权限：view_sysFile_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysFile_del')")
	@PostMapping("/del")
	public R removeSysFile(@RequestBody SysFilePO sysFilePO) {
		try{
		    return R.ok(sysFileService.removeSysFile(sysFilePO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}

	/**
	 * 上传文件 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常
	 * @param file 资源
	 * @return R
	 */
	@ApiOperation(value = "上传文件", notes = "权限：view_sysFile_upload")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysFile_upload')")
	@PostMapping("/upload")
	public R upload(@RequestPart("file") MultipartFile file) {
		try{
			return R.ok(sysFileService.uploadFile(file));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "下载文件", notes = "权限：view_sysFile_download")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysFile_download')")
	// @AnonymousAccess
	@PostMapping("/download")
	public void postDownLoad(HttpServletResponse response,@RequestPart("file") String file) {
		sysFileService.postDownload(response,file);
	}

	@ApiOperation(value = "预览文件：参数[file]要经过Base64 编码", notes = "权限：view_sysFile_download")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysFile_download')")
	// @AnonymousAccess
	@GetMapping("/download/{file}")
	public void getDownLoad(HttpServletResponse response,@PathVariable("file") String file) {
		sysFileService.getDownLoad(response,file);
	}
	
}

